package com.xiaofei.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URI;
import java.net.URL;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;

import com.xiaofei.model.NetModel;
import com.xiaofei.model.TrafficStatModel;
import com.xiaofei.net.HttpGetCon;
import com.xiaofei.net.HttpPostCon;
import com.xiaofei.net.TrafficStatsInfo;
import com.xiaofei.net.URLConection;

public class NetUtils {

	// sync
	public static NetModel getUrlInfoUrlCon(URL url) {
		NetModel mModel = new URLConection(url).getNetInfo();
		return mModel;
	}

	public static NetModel getUrlInfoHttpPost(URI uri, List<NameValuePair> list) {
		NetModel mModel = new HttpPostCon(uri, list).getNetInfo();
		return mModel;
	}

	public static NetModel getUrlInfoHttpGet(URI uri) {
		NetModel mModel = new HttpGetCon(uri).getNetInfo();
		return mModel;
	}

	public static TrafficStatModel getTrafficStats(int uid) {
		TrafficStatModel mModel = new TrafficStatsInfo(uid).getStatModel();
		return mModel;
	}

	public static TrafficStatModel getTrafficStats() {
		TrafficStatModel mModel = new TrafficStatsInfo().getStatModel();
		return mModel;
	}

	public static boolean NetmodleFileStorage(List<NetModel> models) {

		ObjectOutputStream oos;
		try {
			new File("mnt/sdcard/netmodel/netmodel.txt").createNewFile();

			FileOutputStream fos = new FileOutputStream(
					"mnt/sdcard/netmodel/netmodle.txt");
			oos = new ObjectOutputStream(fos);
			oos.writeObject(models);
			oos.close();
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

	}

	public static List<NetModel> NetmodleFileRead() {

		ObjectInputStream ois;
		try {
			FileInputStream fis = new FileInputStream(
					"mnt/sdcard/netmodel/netmodle.txt");
			ois = new ObjectInputStream(fis);
			// NetModel modle = (NetModel)ois.readObject();
			List<NetModel> modles = (List<NetModel>) ois.readObject();
			return modles;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

	public static JSONArray jsonArray;
	public static JSONArray NetModelJSON(String url,NetModel model) {
		 
		if (jsonArray == null) {
			jsonArray = new JSONArray();
		}
		try {
			JSONObject jObject = new JSONObject();
			jObject.put(url,model.toString());
			jsonArray.put(jObject);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonArray;

	}

}
